<?php

namespace app\admin\validate\finance;

use think\Validate;

class Spendingbudget extends Validate
{
    /**
     * 验证规则
     */
    protected $rule = [
        'yer' => 'length:4',
        'mth' => 'between:1,12|checkUnique',
        'amount' => 'egt:0|number',
    ];

    /**
     * 提示消息
     */
    protected $message = [

    ];

    /**
     * 验证场景
     */
    protected $scene = [
        'add'  => ['yer', 'mth', 'amount'],
        'edit' => ['yer', 'mth', 'amount'],
    ];

    //自定义验证规则
    protected function checkUnique($value, $rule, $data=[])
    {
        $where = [];
        if (isset($data['id'])) {
            $where['id'] = ['<>', $data['id']];
        }
        $where['yer'] = $data['yer'];
        $where['mth'] = $data['mth'];
        if (\think\Db::name('finance_spending_budget')->where($where)->count()) {
            return $data['yer'] . '年' . $data['mth'] . '月份已经存在！';
        }
        return true;
    }

    public function __construct(array $rules = [], $message = [], $field = [])
    {
        $this->field = [
            'yer' => __('Yer'),
            'mth' => __('Mth'),
            'amount' => __('Amount'),
        ];
        parent::__construct($rules, $message, $field);
    }
}
